package com.bjesc_b2c.app.appraiset.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.utils.PopupWindowScreenBrightnessUtils;
import com.rxretrofit_build.model.RxBusBean;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rxbus_base.RxBus;

/**
 * Created by Jason Chen on 2017/2/21.
 */

public abstract class BasePopupWindow extends PopupWindow {
    protected int popupWidth;
    protected int popupHeight;
    protected Context context;
    protected LayoutInflater inflater;
    protected View view;
    protected int rootViewId;
    protected OnPopupShowOnFinishListener showOnFinishListener;
    protected View.OnClickListener listener;
    private Subscription subscription;
    public BasePopupWindow(Context context,View.OnClickListener listener) {
        this.listener = listener;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        initViews();
        rootViewId = setRootViewId();
        setView();
        initRxBus(setOnNext());
    }
    private void initRxBus(final Action1<Object> onNext) {
        if (onNext != null) {
            subscription = RxBus
                    .getInstance()
                    .toObserverable()
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .onBackpressureDrop()
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(onNext);
        }
    }

    private Action1<Object> setOnNext() {
        return new Action1<Object>() {
            @Override
            public void call(Object o) {
                if(o instanceof RxBusBean){
                    RxBusBean rxBusBean = (RxBusBean) o;
                    if(!TextUtils.isEmpty(rxBusBean.getRequestMark())){
                        doOnNext(rxBusBean);
                    }
                }
            }
        };
    }
    protected  void doOnNext(RxBusBean rxBusBean){

    }
    protected abstract void initViews();

    protected abstract int setRootViewId();

    protected void setAnim(int styleId){
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.popupwindow_bottomtotop_anim);
    }

    protected void setView() {
        // 设置SelectPicPopupWindow的View
        this.setContentView(view);
         //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(
                R.color.color00000000));
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = view.findViewById(rootViewId).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
        //获取自身的长宽高
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupHeight = view.getMeasuredHeight();
        popupWidth = view.getMeasuredWidth();

    }

    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        if (this.showOnFinishListener != null) {
            this.showOnFinishListener.onShow();
        }
        PopupWindowScreenBrightnessUtils.setBrightness(context,this);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        super.showAsDropDown(anchor, xoff, yoff, gravity);
        if (this.showOnFinishListener != null) {
            this.showOnFinishListener.onShow();
        }
        PopupWindowScreenBrightnessUtils.setBrightness(context,this);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        super.showAsDropDown(anchor, xoff, yoff);
        if (this.showOnFinishListener != null) {
            this.showOnFinishListener.onShow();
        }
        PopupWindowScreenBrightnessUtils.setBrightness(context,this);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        if (this.showOnFinishListener != null) {
            this.showOnFinishListener.onShow();
        }
        PopupWindowScreenBrightnessUtils.setBrightness(context,this);
    }
    public void showUp(View v) {
        //获取需要在其上方显示的控件的位置信息
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        //在控件上方显示
        showAtLocation(v, Gravity.NO_GRAVITY, (location[0]) , location[1] - popupHeight);
    }

    /**
     * 设置显示在v上方（以v的中心位置为开始位置）
     * @param v
     */
    public void showUp2(View v) {
        //获取需要在其上方显示的控件的位置信息
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        //在控件上方显示
        showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
    }
    @Override
    public void dismiss() {
        super.dismiss();
        if (this.showOnFinishListener != null) {
            this.showOnFinishListener.onFinish();
        }
        PopupWindowScreenBrightnessUtils.setRecovery(context,this);
    }

    public void setShowOnFinishListener(OnPopupShowOnFinishListener showOnFinishListener) {
        this.showOnFinishListener = showOnFinishListener;
    }

    public  interface OnPopupShowOnFinishListener {
        void onFinish();
        void onShow();
    }

}
